//*********************************************************************** // k_neighbors.h // // this is an implementationof the K-nearest neighbors classification // algorithm. it accepts any number of attributes and measures the // neighborhood distance using the square root of the sum of the // attribute distances squared. // // INPUTS: (from disk file) // control.dat - control file - space delimited // k value & datafile name // // datafile.clas - classification set // attribute count - don't include the classification in // the count // data - space delimited // // datafile.test - test set // attribute count - no classification data // data - space delimited // // OUTPUTS: (to disk file) // datafile.out - space delimited // test instance // k-nearest classification neighbor instances // //*********************************************************************** // WARNING: none // //*********************************************************************** // IMPLEMENTATION NOTE: all files are in the executable working directory // //*********************************************************************** // created by: j. aleshunas // created on: 18 nov 04 // modified on: 23 nov 04 // // © 2004 John Aleshunas // //*********************************************************************** #include #include #include #include #include using namespace std; //*********************************************************************** // class Classification_instance declaration //*********************************************************************** class Classification_instance { // private class variables // private methods public: // public class variables vector vfAttributes; string sClassification; // public methods Classification_instance(void); // constructor //Classification_instance(string sAlphabet); // constructor }; // class Classification_instance //*********************************************************************** // class Classification_instance method declarations //*********************************************************************** // class Classification_instance constructor Classification_instance::Classification_instance(void){ // make room for the attributes //vfAttributes.resize(iAttribute_ct); return; } //Classification_instance::Classification_instance //*********************************************************************** // class Classification_set declaration //*********************************************************************** class Classification_set { // private class variables // private methods public: // public class variables vector vclInstances; // public methods //Classification_set(void); // constructor Classification_set(void); // constructor //Classification_set(string sAlphabet); // constructor void Read_data(string sFilename); void Write_data(string sFilename); }; // class Classification_set //*********************************************************************** // class Classification_set method declarations //*********************************************************************** // class Classification_set constructor Classification_set::Classification_set(void) { return; } //Classification_set::Classification_set //*********************************************************************** void Classification_set::Read_data(string sFilename){ // local variables int iAttribute_ct; int iIndex; // declare an input stream to read the key ifstream strInput_stream; // modify the filename sFilename = sFilename + ".clas"; // declare data storage for the input data Classification_instance clInput_instance; // open the input stream to read the key strInput_stream.open(sFilename.c_str()); // check if the file was OK if (strInput_stream.is_open()){ // read the count of attributes strInput_stream >> iAttribute_ct; // set the size of the attributes vector clInput_instance.vfAttributes.resize(iAttribute_ct); while(!strInput_stream.eof()){ // get the attributes for(iIndex = 0; iIndex < iAttribute_ct; iIndex++){ strInput_stream >> clInput_instance.vfAttributes[iIndex]; } // for // read the classification name strInput_stream >> clInput_instance.sClassification; // save the data in the vector set vclInstances.push_back(clInput_instance); }// while } //if else cout << "Error reading the key file!" << endl << endl; // print error message strInput_stream.close(); // close filestream return; } //Classification_set::Read_data //*********************************************************************** void Classification_set::Write_data(string sFilename){ // local variables unsigned uIndex1, uIndex2; // modify the filename sFilename = sFilename + "_cl.out"; // declare an output stream ofstream strResults_out_stream; // open the stream to write the output plaintext strResults_out_stream.open(sFilename.c_str()); for(uIndex1 = 0; uIndex1 < vclInstances.size(); uIndex1++){ for(uIndex2 = 0; uIndex2 < vclInstances[uIndex1].vfAttributes.size(); uIndex2++){ strResults_out_stream << vclInstances[uIndex1].vfAttributes[uIndex2]; strResults_out_stream << " "; } // for // write the plaintext to the output file stream strResults_out_stream << vclInstances[uIndex1].sClassification; strResults_out_stream << endl << endl; } // for strResults_out_stream.close(); return; } // lassification_set::Write_data //*********************************************************************** // class Test_set declaration //*********************************************************************** class Test_set { // private class variables // private methods public: // public class variables vector vclInstances; // public methods Test_set(void); // constructor //Test_set(string sAlphabet); // constructor void Read_data(string sFilename); void Write_data(string sFilename); }; // class Test_set //*********************************************************************** // class Classification_set constructor Test_set::Test_set(void) { return; } //Classification_set::Classification_set //*********************************************************************** void Test_set::Read_data(string sFilename){ // local variables int iAttribute_ct; int iIndex; // declare an input stream to read the data ifstream strInput_stream; // modify the filename sFilename = sFilename + ".test"; // declare data storage for the input data Classification_instance clInput_instance; // open the input stream to read the key strInput_stream.open(sFilename.c_str()); // check if the file was OK if (strInput_stream.is_open()){ // read the count of attributes strInput_stream >> iAttribute_ct; // set the size of the attributes vector clInput_instance.vfAttributes.resize(iAttribute_ct); while(!strInput_stream.eof()){ // get the attributes for(iIndex = 0; iIndex < iAttribute_ct; iIndex++){ strInput_stream >> clInput_instance.vfAttributes[iIndex]; } // for // no need to read the classification name // save the data in the vector set vclInstances.push_back(clInput_instance); }// while } //if else cout << "Error reading the key file!" << endl << endl; // print error message strInput_stream.close(); // close filestream return; } //Test_set::Read_data //*********************************************************************** void Test_set::Write_data(string sFilename){ // local variables unsigned uIndex1, uIndex2; // modify the filename sFilename = sFilename + "_ts.out"; // declare an output stream ofstream strResults_out_stream; // open the stream to write the output plaintext strResults_out_stream.open(sFilename.c_str()); for(uIndex1 = 0; uIndex1 < vclInstances.size(); uIndex1++){ for(uIndex2 = 0; uIndex2 < vclInstances[uIndex1].vfAttributes.size(); uIndex2++){ strResults_out_stream << vclInstances[uIndex1].vfAttributes[uIndex2]; strResults_out_stream << " "; } // for // write the plaintext to the output file stream strResults_out_stream << vclInstances[uIndex1].sClassification; strResults_out_stream << endl << endl; } // for strResults_out_stream.close(); return; } // Test_set::Write_data //*********************************************************************** // class Neighbor_instance declaration //*********************************************************************** class Neighbor_instance { // private class variables // private methods public: // public class variables vector vfAttributes; string sClassification; float fNeighbor_distance; // public methods Neighbor_instance(void); // constructor //Classification_instance(string sAlphabet); // constructor }; // class Neighbor_instance //*********************************************************************** // class Neighbor_instance method declarations //*********************************************************************** // class Neighbor_instance constructor Neighbor_instance::Neighbor_instance(void){ // make room for the attributes //vfAttributes.resize(iAttribute_ct); return; } //Neighbor_instance::Neighbor_instance //*********************************************************************** // Non class functions //*********************************************************************** void Read_control_file(unsigned &uK_count, string &sInput_filename) { // declare an input stream to read the data ifstream strInput_stream; // modify the filename string sFilename = "control.dat"; // open the input stream to read the key strInput_stream.open(sFilename.c_str()); // check if the file was OK if (strInput_stream.is_open()){ // read the count of neighbors strInput_stream >> uK_count; // read the filenames strInput_stream >> sInput_filename; } //if else cout << "Error reading the key file!" << endl << endl; // print error message strInput_stream.close(); // close filestream return; } // Read_control_file